package cn.school.service;

import cn.school.dao.StudentDao;
import cn.school.entity.Page;
import cn.school.entity.Student;
import cn.school.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentService {
    public static final StudentService INST=new StudentService();

    int add(Student stu){
        SqlSession s = MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.add(stu);
        } finally {
            MyBatisUtil.close(s);
        }
    }

    //根据主键改其他列，一般只改一个实体
    int update(Student stu){
        SqlSession s = MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.update(stu);
        } finally {
            MyBatisUtil.close(s);
        }
    }

//    public int updatePass(@Param("stuno") long stuno, @Param("newPass") String newPass, @Param("oldPass") String oldPass);

    int del(Student stu){
        SqlSession s = MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.del(stu);
        } finally {
            MyBatisUtil.close(s);
        }
    }

    //根据主键，读取其他的信息
    Student get(long stuno){
        SqlSession s = MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.get(stuno);
        } finally {
            MyBatisUtil.close(s);
        }
    }


    /**
     * 查询条件，从第三个开始，如果没有，则忽略
     * @param pageNo
     * @param pageSize
     * @param sname
     * @param qq
     * @param telephone
     * @return
     */
    public Page<Student> search(int pageNo, int pageSize, String sname, String qq, String telephone, Date start, Date end){
        SqlSession s = MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            List<Student> list = dao.search(pageNo, pageSize, sname, qq, telephone,start,end);
            int total=dao.searchTotal(sname, qq, telephone,start,end);
            return new Page<Student>(pageNo,pageSize,total,list);
        } finally {
            MyBatisUtil.close(s);
        }
    }
}
